package com.zty.concurrency.example.sync;

import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class SynchronizedExample1 {

    public void  test(int j ){
        synchronized (this){
            for (int i = 0; i <10 ; i++) {
                log.info("test {} - {}",j,i);
            }
        }
    }
    public synchronized void test2(int j){
        for (int i = 0; i <10 ; i++) {
            log.info("test2 {} - {}",j,i);
        }
    }
    public  static  void  main(String[] args){
        SynchronizedExample1 example1= new SynchronizedExample1();
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(
                ()->{
                    example1.test(1);
                });
        executorService.execute(
                ()->{
                    example1.test2(2);
                });
    }
}
